﻿package com.game.api
{
	
	import api.dataStruct.InitAPIObj;
	import api.event.CallFunName;
	import api.event.CustomEvent;
	import api.event.EventData;
	import api.event.EventSendObj;
	import api.event.GameAPIEvent;
	
	import com.game.utils.LoadSwf;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	
	public class APICore
	{
		private static var _apiScore:APICore;
		private var _api:DisplayObjectContainer
		private var _initAPIObj:InitAPIObj;
		private var _currClass:Object;
		private var _sp:Sprite;
		private var _isAddChild:Boolean=false;
		private var _loadSwf:LoadSwf=new LoadSwf();
		private var _isRobot:Boolean=false;
		
		public function get isRobot():Boolean
		{
			return _isRobot;
		}
		
		public function set isRobot(value:Boolean):void
		{
			_isRobot = value;
		}
		
		public function get currClass():Object
		{
			return _currClass;
		}
		
		public function set currClass(value:Object):void
		{
			_currClass = null;
			_currClass = value;
		}
		
		public function APIScore():void
		{
			if(_apiScore!=null)throw new Error("实例已经存在！请使用getInstance()方法获得到对象！");
			else _apiScore=this;
		}
		
		public static function getInstance():APICore{
			if(_apiScore==null){
				_apiScore=new APICore();
			}
			return _apiScore;
		}
		
		public function init(sp:Sprite,apiSwf:LoaderInfo):void{
			
			_sp=sp;
			_api=(apiSwf.content as DisplayObjectContainer);
			_sp.addChild(_api);;
			setAPI(apiSwf);
		}
		public function setAPI(apiSwf:LoaderInfo):void{
			_api=(apiSwf.content as DisplayObjectContainer);
			_api.addEventListener(GameAPIEvent.PVP_API_TO_GAME,onAPIEvent);
		}
		public function startAPI(initAPIObj:InitAPIObj):void{
			_initAPIObj=initAPIObj;
			callAPIFun(CallFunName.A_INIT_API,_initAPIObj);
		}
		
		//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//Game_A>>API_A:
		
		//
		/**
		 * 
		 * @param proxyName
		 * @param data
		 */
		public function callAPIFun(funName:String,data:*=null):void{
			_api.dispatchEvent(new CustomEvent(GameAPIEvent.PVP_GAME_TO_API,new EventData(funName,data)));
		}
		
		//Game_A>>API_B
		/**
		 * 
		 * @param proxyName
		 * @param data
		 */
		public function sendToAPIFun(eventType:String,isToMe:uint=0,data:*=null):void
		{
			callAPIFun("sendToAPIFun",new EventSendObj(eventType,isToMe,data));
		}
		
		/**
		 * Game_A调用Game_B的方法
		 * @param eventType 方法名称,必须为公开方法
		 * @param isToMe 该消息是否发送给自己
		 * @param data 消息数据
		 */
		public function sendToGameFun(eventType:String,isToMe:uint=0,data:*=null):void
		{
			if(_isRobot){
				return;
			}
			callAPIFun("gameSendToTable",new EventSendObj("onAPICallFun",isToMe,new EventData(eventType,data)));
			
		}
		/**
		 * Game_A设置Game_B的参数
		 * @param eventType 参数名称,必须为公开参数
		 * @param isToMe 该消息是否发送给自己
		 * @param data 消息数据
		 */
		public function sendToGameProperty(eventType:String,isToMe:uint=0,data:*=null):void
		{
			if(_isRobot){
				return;
			}
			callAPIFun("gameSendToTable",new EventSendObj("onAPISetProperty",isToMe,new EventData(eventType,data)));
		}
		
		//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//API_A>>Game_A:
		
		
		public function onAPICallFun(data:EventData):void{
			
			if(_currClass.hasOwnProperty(data.type)){
				_currClass[data.type](data.data);
			}else{
				trace("无法找到属性:_currClass:"+_currClass+" type:"+data.type+" data:"+data.data);
			}
		}
		public function onAPISetProperty(data:EventData):void{
			if(_currClass.hasOwnProperty(data.type)){
				_currClass[data.type]=data.data;
			}else{
				trace("无法找到属性:_currClass:"+_currClass+" type:"+data.type+" data:"+data.data);
			}
		}
		
		private function onAPIEvent(e:CustomEvent):void{
			if(!e.data.type){
				trace("myEvent.data.type不能为空!!!");
				return;
			}
			
			var data:EventData=e.data as EventData;
			
			if(!this.hasOwnProperty(data.type)){
				trace(this+"无法找到属性:"+data.type)
				return;
			}
			this[data.type](data.data);
		}
		
	}
};